#include "serialport.h"
#include "ui_serialport.h"

serialPort::serialPort(QWidget *parent) :
    QDialog(parent),isValic(false),
    ui(new Ui::serialPort)
{
    ui->setupUi(this);
    setFixedSize(width(),height());
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->comboBoxPort->addItem(info.portName());
    }
    connect(ui->comboBoxBaud,SIGNAL(currentIndexChanged(int)),this,SLOT(serialBaud()));
    connect(ui->pushButtonScan,SIGNAL(clicked(bool)),this,SLOT(serialScan()));
    connect(ui->pushButtonOpen,SIGNAL(clicked(bool)),this,SLOT(serialOpen()));
    connect(ui->pushButtonSend,SIGNAL(clicked(bool)),this,SLOT(serialSend()));
}

serialPort::~serialPort()
{
    delete ui;
}

void serialPort::serialScan()
{
    ui->comboBoxPort->clear();
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->comboBoxPort->addItem(info.portName());
    }
}

void serialPort::serialBaud()
{
    if(isValic)serialport->setBaudRate(ui->comboBoxBaud->currentText().toInt());
}

void serialPort::serialOpen()
{
    if(!isValic)
    {
        QString portname = ui->comboBoxPort->currentText();
        QSerialPortInfo portInfo(portname);
        if(portInfo.isNull())
        {
            QMessageBox::information(this,"error","serial isn't existed!","ok");
            return;
        }
        if(portInfo.isBusy())
        {
            QMessageBox::information(this,"error","serial is already openned!","ok");
            return;
        }
        serialport = new QSerialPort;
        serialport->setPortName(portname);
        serialport->open(QIODevice::ReadWrite);
        serialport->setBaudRate(ui->comboBoxBaud->currentText().toInt());
        serialport->setDataBits(QSerialPort::Data8);
        serialport->setParity(QSerialPort::NoParity);
        serialport->setStopBits(QSerialPort::OneStop);
        serialport->setFlowControl(QSerialPort::NoFlowControl);
        connect(serialport,SIGNAL(readyRead()),this,SLOT(serialRead()));
        connect(serialport,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(serialError()));
        ui->pushButtonOpen->setText("close");
        ui->comboBoxPort->setEnabled(false);
        ui->pushButtonSend->setEnabled(true);
        isValic = true;
    }
    else
    {
        serialport->close();
        delete serialport;
        ui->pushButtonOpen->setText("open");
        ui->comboBoxPort->setEnabled(true);
        ui->pushButtonSend->setEnabled(false);
        isValic = false;
    }
}

void serialPort::serialRead()
{
    dataRx.clear();
    dataRx = serialport->readAll();
    ui->textEditRecv->append(dataRx);
    emit ReadyRecv(dataRx);
}

void serialPort::serialSend()
{
    dataTx.clear();
    dataTx = ui->lineEditSend->text().toLatin1();
    SendPackets(dataTx);
}

void serialPort::SendPackets(QByteArray &data){
    serialport->write(data);
}

void serialPort::serialError()
{
    QMessageBox::information(this,"error",serialport->errorString(),"ok");
    ui->pushButtonOpen->setText("open");
    ui->comboBoxPort->setEnabled(true);
    ui->pushButtonSend->setEnabled(false);
    isValic = false;
    ui->comboBoxPort->clear();
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->comboBoxPort->addItem(info.portName());
    }
}
